<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:50
 */
namespace app\shop\controller;
use think\Controller;
use think\Db;
class Login extends Controller
{
    /**
     * 登录页面
     * @return mixed
     */
    public function indexLogin(){
        return $this->fetch('login/index_login');
    }
    /**
     * 用户协议
     * @return mixed
     */
    public function goxieyi(){
        $xieyi = Db::name('about_us')
            ->where('about_id',1)->value('shop_agreement');
        return $this->fetch('login/xieyi',['xieyi'=>$xieyi]);
    }

    /**
     * 登录操作
     * @return \think\response\Json
     */
    public function doLogin(){
        extract(input());
        $shop = Db::name('shops')
            ->where('s_phone',$phone)
            ->field(['s_id','s_name','s_thumb','s_image','s_jianjie','s_task_num','s_money','s_phone','s_status','s_reason','s_password','s_time','s_zizhi','s_three','s_contact_name','s_contact_phone','s_code','s_sta','s_auth'])
            ->find();
        $shop_son = Db::name('role_name')
            ->where('role_user',$phone)
            ->field(['role_status','f_id','r_s_id','role_password'])
            ->find();
        if($shop_son){
            if($shop_son['role_password'] != md5($password)){
                return json(['code'=>-3,'msg'=>'密码错误']);
            }
            if($shop_son['role_status'] != 1){
                return json(['code'=>-2,'msg'=>'该账号已被冻结']);
            }
            $shop = Db::name('shops')
                ->where('s_id',$shop_son['r_s_id'])
                ->field(['s_id','s_name','s_thumb','s_image','s_jianjie','s_task_num','s_money','s_phone','s_status','s_reason','s_password','s_time','s_zizhi','s_three','s_contact_name','s_contact_phone','s_code','s_sta'])
                ->find();
            $shop['s_auth'] = Db::name('role')
                ->where('role_id',$shop_son['f_id'])
                ->value('role_rule');
            $shop['s_password'] = $shop_son['role_password'];
        }
        if(empty($shop)){
            return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
        }
        if($shop['s_sta'] == 2){
            return json(['code'=>-2,'msg'=>'该商户已被冻结']);
        }
        if($shop['s_password'] != md5($password)){
            return json(['code'=>-3,'msg'=>'密码错误']);
        }
        session('shop_id',$shop['s_id']);//商户id
        session('shop_name',$shop['s_name']);//商户名称
        session('shop_thumb',$shop['s_thumb']);//商户头像
        session('shop_auth',$shop['s_auth']);//商户头像
        session('login_phone',$phone);//商户头像
        return json(['code' => 200, 'url' => url('shop/Index/tongji'), 'msg' => '登录成功！']);
    }

    /**
     * 找回密码
     * @return mixed|\think\response\Json
     */
    public function resetPwd(){
        if(request()->isAjax ()){
            extract(input());
            $user = Db::name('role_name')
                ->where('role_user',$phone)
                ->field(['role_name_id','role_user'])
                ->find();
            if($user){
                $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
                if(empty($d)){
                    return json(['code'=>-2,'msg'=>'请先发送验证码']);
                }
                $t = time() - $d['time2'];
                if($t >= 600){
                    return json(['code'=>-2,'msg'=>'验证码失效']);
                }
                if($code != $d['code']){
                    return json(['code'=>-2,'msg'=>'验证码错误']);
                }
                // 启动事务
                Db::startTrans();
                try{
                    Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
                    Db::name('role_name')->where(['role_user'=>$phone])->update(['password'=>$pwd,'role_password'=>md5($pwd),'role_time'=>date('Y-m-d H:i:s')]);
                    // 提交事务
                    Db::commit();
                    return json(['code'=>200,'url'=>url('shop/Login/indexLogin'),'msg'=>'密码重置成功']);
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return json(['code'=>100,'msg'=>'密码重置失败']);
                }
            }else{
                $user = Db::name('shops')
                    ->where('s_phone',$phone)
                    ->field(['s_id','s_phone'])
                    ->find();
                if(empty($user)){
                    return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
                }
                $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
                if(empty($d)){
                    return json(['code'=>-2,'msg'=>'请先发送验证码']);
                }
                $t = time() - $d['time2'];
                if($t >= 600){
                    return json(['code'=>-2,'msg'=>'验证码失效']);
                }
                if($code != $d['code']){
                    return json(['code'=>-2,'msg'=>'验证码错误']);
                }
                // 启动事务
                Db::startTrans();
                try{
                    Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
                    Db::name('shops')->where(['s_phone'=>$phone])->update(['s_password'=>md5($pwd),'s_update_time'=>date('Y-m-d H:i:s')]);
                    // 提交事务
                    Db::commit();
                    return json(['code'=>200,'url'=>url('shop/Login/indexLogin'),'msg'=>'密码重置成功']);
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return json(['code'=>100,'msg'=>'密码重置失败']);
                }
            }

        }
        return $this->fetch ('login/reset_pwd');
    }

    /**
     * 快捷登录
     * @return mixed|\think\response\Json
     */
    public function quickLogin(){
        extract(input());
        $shop = Db::name('shops')
            ->where('s_phone',$phone)
            ->field(['s_id','s_name','s_thumb','s_image','s_jianjie','s_task_num','s_money','s_phone','s_status','s_reason','s_password','s_time','s_zizhi','s_three','s_contact_name','s_contact_phone','s_code','s_sta'])
            ->find();
        $shop_son = Db::name('role_name')
            ->where('role_user',$phone)
            ->field(['role_status','f_id','r_s_id','role_password'])
            ->find();
        if($shop_son){
            if($shop_son['role_status'] != 1){
                return json(['code'=>-2,'msg'=>'该账号已被冻结']);
            }
            $shop = Db::name('shops')
                ->where('s_id',$shop_son['r_s_id'])
                ->field(['s_id','s_name','s_thumb','s_image','s_jianjie','s_task_num','s_money','s_phone','s_status','s_reason','s_password','s_time','s_zizhi','s_three','s_contact_name','s_contact_phone','s_code','s_sta'])
                ->find();
            $shop['s_auth'] = Db::name('role')
                ->where('role_id',$shop_son['f_id'])
                ->value('role_rule');
        }
        if(empty($shop)){
            return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
        }
        if($shop['s_sta'] == 2){
            return json(['code'=>-1,'msg'=>'该商户已被冻结']);
        }
        $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
        if(empty($d)){
            return json(['code'=>-2,'msg'=>'请先发送验证码']);
        }
        $t = time() - $d['time2'];
        if($t >= 600){
            return json(['code'=>-2,'msg'=>'验证码失效']);
        }
        if($code != $d['code']){
            return json(['code'=>-2,'msg'=>'验证码错误']);
        }
        // 启动事务
        Db::startTrans();
        try{
            Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
            // 提交事务
            Db::commit();
            session('shop_id',$shop['s_id']);//商户id
            session('shop_name',$shop['s_name']);//商户名称
            session('shop_thumb',$shop['s_thumb']);//商户头像
            session('shop_auth',$shop['s_auth']);//商户头像
            session('login_phone',$phone);//商户头像
            return json(['code'=>200,'url' => url('shop/Task/homePage'),'msg'=>'登录成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code'=>100,'msg'=>'登录失败']);
        }
    }

    /**
     * 注册
     * @return mixed|\think\response\Json
     */
    public function register(){
        if(request()->isAjax ()){
            extract(input());
            $user = Db::name('shops')
                ->where('s_phone',$phone)
                ->field(['s_id','s_phone'])
                ->find();
            $shop_son = Db::name('role_name')
                ->where('role_user',$phone)
                ->field(['role_status','f_id','r_s_id','role_password'])
                ->find();
            if(!empty($user) || $shop_son){
                return json(['code'=>-1,'msg'=>'该手机号已注册']);
            }
            $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
            if(empty($d)){
                return json(['code'=>-2,'msg'=>'请先发送验证码']);
            }
            $t = time() - $d['time2'];
            if($t >= 600){
                return json(['code'=>-2,'msg'=>'验证码失效']);
            }
            if($code != $d['code']){
                return json(['code'=>-2,'msg'=>'验证码错误']);
            }
            // 启动事务
            Db::startTrans();
            try{
                Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
                $ins = ['s_phone'=>$phone,
                    's_password'=>md5($pwd),
                    's_time'=>date('Y-m-d H:i:s'),
                    's_china'=>$ex,
                    's_num'=>time() . rand(1000,9999)
                ];
                Db::name('shops')->insert($ins);
                // 提交事务
                Db::commit();
                return json(['code'=>200,'url' => url('shop/Task/homePage'),'msg'=>'注册成功']);
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                return json(['code'=>100,'msg'=>'注册失败']);
            }
        }
        return $this->fetch ('login/register');
    }

    /**
     * 发送验证码
     * @return \think\response\Json
     */
    public function sendCode(){
        extract(input());
        if($type == 4 || $type == 5){ //4是换密码，5是换手机给旧手机发，6是换手机号给新手机发，都是登陆后才有的操作
            $phone = session('login_phone');
        }
        if($type == 6){ //4是换密码，5是换手机给旧手机发，6是换手机号给新手机发，都是登陆后才有的操作
            $have = Db::name('shops')
                ->where('s_phone',$phone)
                ->field(['s_id','s_phone'])
                ->find();
            $have2 = Db::name('role_name')
                ->where('role_user',$phone)
                ->field(['role_status','f_id','r_s_id','role_password'])
                ->find();
            if($have || $have2){
                return json(['code'=>-1,'msg'=>'该手机号已注册']);
            }
        }
        if(empty($phone)){
            return json(['code'=>-1,'msg'=>'手机号不能为空']);
        }
        $user = Db::name('shops')
            ->where('s_phone',$phone)
            ->field(['s_id','s_phone'])
            ->find();
        $shop_son = Db::name('role_name')
            ->where('role_user',$phone)
            ->field(['role_status','f_id','r_s_id','role_password'])
            ->find();
        //$type == 1代表登录&找回密码  为2代表注册
        if(empty($user) && $type == 1 && empty($shop_son)){
            return json(['code'=>-1,'msg'=>'该手机号尚未注册']);
        }
        if(!empty($user) && $type == 2){
            return json(['code'=>-1,'msg'=>'该手机号已注册']);
        }
        if(!empty($shop_son) && $type == 2){
            return json(['code'=>-1,'msg'=>'该手机号已注册']);
        }
        $u = Db::name('shop_phonecode')
            ->where('phone',$phone)
            ->find();
        $code = GetRandCode(6);
        if(isset($ex) && $ex != '86'){
            $phone = '00' . $ex . $phone;
        }
        $res = YzxSms($code,$phone);
        if($res['code'] == 000000){
            if(!empty($u)){
                Db::name('shop_phonecode')->where('phone',$phone)->update(['code'=>$code,'time2'=>time(),'status'=>0]);
            }else{
                Db::name('shop_phonecode')->insert(['phone'=>$phone,'code'=>$code,'time2'=>time(),'status'=>0]);
            }
            return json(['code'=>200,'msg'=>'验证码发送成功，请注意查收']);
        }else{
            return json(['code'=>100,'msg'=>'验证码发送失败，请重试']);
        }

    }

    /*
     * 退出登录
     */
    public function loginOut(){
        session(null);
        $this->redirect(url('shop/login/indexLogin'));
    }
}